package edu.qd.learn.newchapter.chapter05.stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 布衣
 */
public class Play {

    public static void main(String[] args) {
        EmployeeData employeeData = new EmployeeData();

        Employee e1 = new Employee(1, "张三", 20, 12001.00, "001");
        employeeData.add(e1);

        e1 = new Employee(2, "李四", 20, 12002.00, "002");
        employeeData.add(e1);

        e1 = new Employee(3, "王二小", 20, 12003.00, "002");
        employeeData.add(e1);

        e1 = new Employee(4, "李春花", 20, 12004.00, "001");
        employeeData.add(e1);

        e1 = new Employee(5, "王小丽", 20, 12005.00, "001");
        employeeData.add(e1);

        //Collectors
//        employeeData.getEmps().stream().collect(Collectors)

        //
        double total = employeeData.getEmps().stream().mapToDouble(Employee::getSalary).sum();
        double total1 = employeeData.getEmps().stream().collect(Collectors.summingDouble(Employee::getSalary));


        Map<String, Double> groupByEmp = employeeData.getEmps()
//        groupingBy(表示分组字段，处理)
                .stream().collect(Collectors.groupingBy(Employee::getDeptNo, Collectors.summingDouble(Employee::getSalary)));

        System.out.println(groupByEmp);

        System.out.println(total);

        //  员工数据的分组操作
        //  按性别统计人数，统计每个部门的人数
        // 统计每个部门的最大工资，最小工资、平均工资、总工资
        Map<String, Double> collect =
                employeeData.getEmps().stream()
                        .collect(
                                Collectors.collectingAndThen(
                                        Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)),
                                        x -> x.map(emp -> Collections.singletonMap(emp.getEname(), emp.getSalary()))
                                                .orElseGet(Collections::emptyMap)
                                )
                        );

        System.out.println(collect);

        Map<String, Map<String, Double>> collect1 = employeeData.getEmps().stream()
                .collect(
                        Collectors.groupingBy(Employee::getDeptNo,
                                Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)),
                                        optional -> optional.map(emp -> Collections.singletonMap(emp.getEname(), emp.getSalary()))
                                                .orElseGet(Collections::emptyMap))
                        )
                );

        System.out.println(collect1);

        Map<String, Double> collect2 = employeeData.getEmps().stream()
                .collect(
                        Collectors.groupingBy(Employee::getDeptNo,
                                Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)),
                                        optional -> optional.get().getSalary()
                                )
                        )
                );

        System.out.println(collect2);


        Map<String, Optional<Employee>>  map = employeeData.getEmps().stream()
                .collect(
                        Collectors.groupingBy(Employee::getDeptNo,
                                Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary))));
        System.out.println(map);

        System.out.println(map.get("002").get().getSalary());

    }


}
